package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.custom.BaseController;
import com.example.demo.entity.CarKeep;
import com.example.demo.entity.MoneyCompany;
import com.example.demo.form.CarKeepForm;
import com.example.demo.form.MoneyCompanyForm;
import com.example.demo.service.MoneyCompanyService;

@RestController
@RequestMapping(value = "/MoneyCompany")
public class MoneyCompanyController extends BaseController<MoneyCompany, Integer, MoneyCompanyForm> {
	@Autowired
	private MoneyCompanyService  moneyCompanyDAO;
	
	@RequestMapping(value="/findbyId")
	public Object byId(CarKeepForm form) {
		MoneyCompany model = null;
		Integer id = form.getId();
		if(id!=null) {
			 model = moneyCompanyDAO.findById(id);
		}	
		return new AjaxResult(model);
		
	}
	@RequestMapping(value = "/onlyNameSave")
	@ResponseBody
	public Object save1(MoneyCompanyForm moneyCompanyForm)  {
		MoneyCompany moneyCompany = new MoneyCompany();
		Integer id = moneyCompanyForm.getId();
		if(id!=null) {
			moneyCompany = moneyCompanyDAO.findById(id);
		}
		List list = new ArrayList();
		for (MoneyCompany moneyCompany1: moneyCompanyDAO.findAll()) {
			String aaa = moneyCompany1.getName();
			list.add(aaa);
		}
		String f = moneyCompanyForm.getName();
		for (int i = 0; i < list.size(); i++) {
			String bbb = (String) list.get(i);
			if(bbb.equals(f)) {	
				return new AjaxResult( false,"名称重复");
			}
		}
		BeanUtils.copyProperties(moneyCompanyForm, moneyCompany,"id");
		moneyCompanyDAO.save(moneyCompany);
		return new AjaxResult("数据保存成功");
	}
}
